'''
Description: 
Author: Zilu
Date: 2021-11-06 00:02:18
Version: 
LastEditTime: 2021-11-06 17:02:51
LastEditors: Zilu
'''

# filter 和 map 类似，不过根据函数的返回值真假决定是否保留
def not_empty(s):
    return s and s.strip()

a = filter(not_empty, ['A','B',None,'','C','     '])
b = list(a)

# filter 返回的是Iterator，是惰性序列，不会完成计算，需要将其转换为list获取全部结果

# 埃式筛法计算素数
def _odd_iter():
    n=1
    while True:
        n += 2
        yield n

def _not_divisible(n):
    return lambda x:x%n>0

def primes():
    yield 2
    it = _odd_iter()    #  初始序列
    while True:
        n = next(it)
        yield n
        it = filter(_not_divisible(n),it)

for n in primes():
    if n < 1000:
        print(n)
    else:
        break

# 练习: 使用 filter 筛选出回数
# def is_palindrome(n):
#     def re_construct(n):
#         num = 0
#         while n>0:
#             num = num*10 + n%10
#             n = int(n/10)
#         return num
#     if n == re_construct(n):
#         return True
#     else:
#         return False

# def is_palindrome(n):
#     s = str(n)
#     return s == s[::-1]

def is_palindrome(n):
    m = list(map(int,str(n)))
    return m == list(reversed(m))

list(filter(is_palindrome,range(0,200)))

    
